//
//  PPFoundSearchAlbumViewController.m
//  pickpic
//
//  Created by Jinxiao on 8/27/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPFoundSearchAlbumViewController.h"
#import "PPTimelineDetailViewController.h"

#import "PPFoundSearchAlbumTableViewCell.h"

#import "PPAlbumObject.h"

static const NSInteger kCount = 30;
@interface PPFoundSearchAlbumViewController () <PPFoundSearchAlbumTableViewCellDelegate>

@end

@implementation PPFoundSearchAlbumViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    [self.tableView registerNib:[UINib nibWithNibName:@"PPFoundSearchAlbumTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView fd_heightForCellWithIdentifier:@"cell" cacheByIndexPath:indexPath configuration:^(id cell) {

    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return ceil(self.items.count/3.0);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    PPAlbumObject *leftAlbum = [self.items safeObjectAtIndex:(indexPath.row*3)];

    PPAlbumObject *middleAlbum = [self.items safeObjectAtIndex:(indexPath.row*3 + 1)];

    PPAlbumObject *rightAlbum = [self.items safeObjectAtIndex:(indexPath.row*3 + 2)];

    ((PPFoundSearchAlbumTableViewCell *)cell).leftAlbum = leftAlbum;
    ((PPFoundSearchAlbumTableViewCell *)cell).middleAlbum = middleAlbum;
    ((PPFoundSearchAlbumTableViewCell *)cell).rightAlbum = rightAlbum;
    ((PPFoundSearchAlbumTableViewCell *)cell).delegate = self;

    return cell;
}

#pragma mark - Timeline

- (void)fetchNewItems {
    [super fetchNewItems];

    __weak typeof(self)weakSelf = self;

    [PPNetworkService searchWithPage:@1 limit:@(kCount) keyword:_keyword callback:^(id data, NSError *error) {
        __strong typeof(weakSelf)strongSelf = weakSelf;

        if(strongSelf == nil) {
            return;
        }

        [strongSelf didReceiveNewResult:data ? : error];
    }];
}

- (void)fetchMoreItems {
    [super fetchMoreItems];

    __weak typeof(self)weakSelf = self;

    [PPNetworkService searchWithPage:@(self.items.count/kCount + 1) limit:@(kCount) keyword:_keyword callback:^(id data, NSError *error) {
        __strong typeof(weakSelf)strongSelf = weakSelf;

        if(strongSelf == nil) {
            return;
        }

        [strongSelf didReceiveMoreResult:data ? : error];
    }];
}

- (NSArray *)didReceiveNewResult:(id)result {
    NSArray *items = [super didReceiveNewResult:result];

    if([items count] >= kCount) {
        self.scrollWrapper.enableLoadMore = YES;
    } else {
        self.scrollWrapper.enableLoadMore = NO;
    }

    return items;
}

- (NSArray *)didReceiveMoreResult:(id)result {
    NSArray *items = [super didReceiveMoreResult:result];

    if([items count] >= kCount) {
        self.scrollWrapper.enableLoadMore = YES;
    } else {
        self.scrollWrapper.enableLoadMore = NO;
    }

    return items;
}

- (NSArray *)itemsFromResult:(id)result {
    NSMutableArray *items = [NSMutableArray array];

    for(NSDictionary *parameters in [result validArrayForKey:@"albums"]) {
        PPAlbumObject *album = [PPAlbumObject albumForParameters:parameters];
        [items addObject:album];
    }

    return items;
}

#pragma mark - PPFoundSearchAlbumTableViewCellDelegate

- (void)foundSearchAlbumCell:(PPFoundSearchAlbumTableViewCell *)foundSearchAlbumCell didSelectAlbum:(PPAlbumObject *)album {
    [MobClick event:@"139"];
    PPTimelineDetailViewController *detailViewController = [[PPTimelineDetailViewController alloc] init];
    detailViewController.album = album;
    [self.navigationController pushViewController:detailViewController animated:YES];
}

- (UIView *)emptyPromptViewForError:(NSError *)error {
    if(error != nil) {
        UILabel *label = [[UILabel alloc] init];
        label.text = error.networkPrompt;
        label.font = [UIFont systemFontOfSize:14];
        label.textColor = [UIColor colorWithRGBHex:0x999999];
        [label sizeToFit];

        return label;
    } else {
        UILabel *label = [[UILabel alloc] init];
        label.text = @"搜索无结果";
        label.font = [UIFont systemFontOfSize:14];
        label.textColor = [UIColor colorWithRGBHex:0x999999];
        [label sizeToFit];

        return label;
    }
}

- (UIEdgeInsets)preferredScrollInsets {
    return UIEdgeInsetsZero;
}

@end
